<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Class	: M_rptpenjualan
 * 
 * Table	: do
 *  
 * @author Eko Junaidi Salam
 *
 */
class M_rptpenjualan extends CI_Model{

	function __construct(){
		parent::__construct();
	}
	
	/**
	 * Fungsi	: getAll
	 * 
	 * Untuk mengambil all-data
	 * 
	 * @param number $start
	 * @param number $page
	 * @param number $limit
	 * @return json
	 */
	
	function getAll($tglmulai, $tglsampai,$saring,$filters,$start, $page, $limit){
		if($saring == "Range" && $filters == null)
		{
			// GridFilters sends filters as an Array if not json encoded
			if (is_array($filters)) {
				$encoded = false;
			} else {
				$encoded = true;
				$filters = json_decode($filters);
			}

			$where = " (TANGGAL_DO >= DATE('$tglmulai') AND TANGGAL_DO <= DATE('$tglsampai')) ";
			$qs = '';

			// loop through filters sent by client
			if (is_array($filters)) {
				for ($i=0;$i<count($filters);$i++){
					$filter = $filters[$i];

					// assign filter data (location depends if encoded or not)
					if ($encoded) {
						$field = $filter->field;
							
						$value = $filter->value;
						$compare = isset($filter->comparison) ? $filter->comparison : null;
						$filterType = $filter->type;
					} else {
						$field = $filter['field'];
						$value = $filter['data']['value'];
						$compare = isset($filter['data']['comparison']) ? $filter['data']['comparison'] : null;
						$filterType = $filter['data']['type'];
					}

					switch($filterType){
						case 'string' : $qs .= " AND ".$field." LIKE '%".$value."%'"; Break;
						case 'list' :
							if (strstr($value,',')){
								$fi = explode(',',$value);
								for ($q=0;$q<count($fi);$q++){
									$fi[$q] = "'".$fi[$q]."'";
								}
								$value = implode(',',$fi);
								$qs .= " AND ".$field." IN (".$value.")";
							}else{
								$qs .= " AND ".$field." = '".$value."'";
							}
						Break;
						case 'boolean' : $qs .= " AND ".$field." = ".($value); Break;
						case 'numeric' :
							switch ($compare) {
								case 'eq' : $qs .= " AND ".$field." = ".$value; Break;
								case 'lt' : $qs .= " AND ".$field." < ".$value; Break;
								case 'gt' : $qs .= " AND ".$field." > ".$value; Break;
							}
						Break;
						case 'date' :
							switch ($compare) {
								case 'eq' : $qs .= " AND ".$field." = '".date('Y-m-d',strtotime($value))."'"; Break;
								case 'lt' : $qs .= " AND ".$field." < '".date('Y-m-d',strtotime($value))."'"; Break;
								case 'gt' : $qs .= " AND ".$field." > '".date('Y-m-d',strtotime($value))."'"; Break;
							}
						Break;
						case 'datetime' :
							switch ($compare) {
								case 'eq' : $qs .= " AND ".$field." = '".date('Y-m-d H:i:s',strtotime($value))."'"; Break;
								case 'lt' : $qs .= " AND ".$field." < '".date('Y-m-d H:i:s',strtotime($value))."'"; Break;
								case 'gt' : $qs .= " AND ".$field." > '".date('Y-m-d H:i:s',strtotime($value))."'"; Break;
							}
						Break;
					}
				}
				$where .= $qs;
			}
			
			// $sql = "SELECT * FROM TERIMABARANG
			// WHERE ".$where;
			
			$sql = "SELECT A.TANGGAL_DO,C.NOMOR_FAKTUR,A.NAMA_CUSTOMER,B.QUANTITY,B.SATUAN,B.NAMA_BARANG,B.HARGA_SATUAN,B.HARGA_TOTAL,A.PPN,A.MATERAI,
			A.TOTAL FROM (DO as A INNER JOIN DODETAIL as B ON A.NOMOR_DO=B.NOMOR_DO)
			LEFT JOIN FAKTURPAJAK as C ON A.NOMOR_INVOICE=C.NOMOR_INVOICE WHERE ".$where;
			
			//$sql .= " ORDER BY ".$dsort;
			$sql .= " LIMIT ".$start.",".$limit;
			$query = $this->db->query($sql)->result();
			
			$total  = $this->db->query("SELECT COUNT(A.TANGGAL_DO) AS total,C.NOMOR_FAKTUR,A.NAMA_CUSTOMER,B.QUANTITY,B.SATUAN,B.NAMA_BARANG,B.HARGA_SATUAN,B.HARGA_TOTAL,A.PPN,A.MATERAI,
			A.TOTAL FROM (DO as A INNER JOIN DODETAIL as B ON A.NOMOR_DO=B.NOMOR_DO)
			LEFT JOIN FAKTURPAJAK as C ON A.NOMOR_INVOICE=C.NOMOR_INVOICE WHERE ".$where)->result();
			
			$data   = array();
			foreach($query as $result){
				$data[] = $result;
			}
			
			$json	= array(
				'success'   => TRUE,
				'message'   => "Loaded data",
				'total'     => $total[0]->total,
				'data'      => $data
			);
			
			return $json;
		}
	}
}
?>